package com.leyou.item.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entity.Brand;
import com.leyou.item.mapper.BrandMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class BrandService {


    @Autowired
    private BrandMapper brandMapper;

    /**
     * 品牌查询
     * @param page
     * @param rows
     * @param key
     * @param sortBy
     * @param desc
     * @return
     */
    public PageResult<BrandDTO> queryResult(Integer page, Integer rows, String key, String sortBy, Boolean desc) {

        //1.页
        PageHelper.startPage(page, rows);
        //2.搜索
        Example example = new Example(Brand.class);
        if (StringUtils.isNoneBlank(key)) {
            //name LIKE '%H%' or letter = 'X'
            example.createCriteria().orLike("name", "%" + key + "%").orEqualTo("letter", key);
        }

        //3.排序
        if (StringUtils.isNotBlank(sortBy)) {
            String orderByClause = sortBy + (desc ? " DESC" : " ASC");
            example.setOrderByClause(orderByClause);
        }
        //4.查询出结果
        List<Brand> brands = brandMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(brands)){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        //5.封装结果
        PageInfo<Brand> info = new PageInfo<>(brands);

        return new PageResult<>(info.getTotal(), BeanHelper.copyWithCollection(brands,BrandDTO.class));
    }

    /**
     * 品牌新增
     * @param brandDTO
     * @param ids
     */
    @Transactional
    public void saveBrand(BrandDTO brandDTO, List<Long> ids) {
        //新增品牌
        Brand brand = BeanHelper.copyProperties(brandDTO, Brand.class);
        int count = brandMapper.insertSelective(brand);
        if (count!=1){
            //新增失败，抛出异常
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }

        //新增品牌和分类的中间表
        int count1 = brandMapper.insertCategoryBrand(brand.getId(), ids);
        if (count1 != ids.size()){
            //新增失败，抛出异常
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
    }

    /**
     * 品牌删除
     * @param id
     */
    @Transactional
    public void deleteBrand(Long id) {
        //删除品牌
        int count = brandMapper.deleteByPrimaryKey(id);
        if (count != 1){
            throw new LyException(ExceptionEnum.DELETE_OPERATION_FAIL);
        }

        count = brandMapper.deleteBrand(id);
        if (count < 1){
            throw new LyException(ExceptionEnum.DELETE_OPERATION_FAIL);
        }
    }


    /**
     * 品牌修改
     * @param brandDTO
     * @param ids
     */
    @Transactional
    public void updateBrand(BrandDTO brandDTO, List<Long> ids) {
        int count = brandMapper.updateByPrimaryKeySelective(BeanHelper.copyProperties(brandDTO, Brand.class));
        if (count != 1){
            throw new LyException(ExceptionEnum.UPDATE_OPERATION_FAIL);
        }

        count = brandMapper.deleteBrand(brandDTO.getId());
        if (count < 1){
            throw new LyException(ExceptionEnum.DELETE_OPERATION_FAIL);
        }

        count = brandMapper.insertCategoryBrand(brandDTO.getId(), ids);
        if (count < ids.size()){
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
    }

    public BrandDTO queryById(Long brandId) {

        Brand brand = brandMapper.selectByPrimaryKey(brandId);
        if (brand == null){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }

        return BeanHelper.copyProperties(brand,BrandDTO.class);
    }

    public List<BrandDTO> queryByBrandId(Long cid) {

        List<Brand> list = brandMapper.queryByBrandId(cid);
        if (CollectionUtils.isEmpty(list)){

            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }

        return BeanHelper.copyWithCollection(list,BrandDTO.class);
    }

    public List<BrandDTO> queryByIds(List<Long> idList) {

        List<Brand> list = brandMapper.selectByIdList(idList);
        if (CollectionUtils.isEmpty(list)){

            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }

        return BeanHelper.copyWithCollection(list,BrandDTO.class);
    }
}
